package managedbean;

import exception.ExistException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.model.MenuModel;
import session.stateless.SystemMessagesLocal;
import session.stateless.StaffAccountManagerLocal;
import util.entityhelper.StaffAccountState;
import util.enumeration.AccessRight;
import util.enumhelper.SupportedLocale;
import util.enumeration.Country;
import util.enumeration.Department;
import util.enumeration.Gender;
import util.enumeration.JobPosition;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 */
@ManagedBean
@RequestScoped
public class StaffEditAccountManagedBean implements Serializable {
    @EJB
    private StaffAccountManagerLocal staffAccountMgr;
    // EXCEPTION
    @EJB
    private SystemMessagesLocal systemMessages;
    
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private StaffAccountState staffAcctState;
    
    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    
    // fields passed from session
    private Long staffAccountId;
    
    // other fields from database to be displayed
    private String username;
    private String currentPassword;
    private String lastname;
    private String firstname;
    private String gender;
    private String birthdate;
    private String country;
    private String locale;
    private String email;
    private String emailSecondary;
    private Department department;
    private JobPosition jobPosition;
    private String joinDate;
    private List<AccessRight> accessRightsList;
    
    private String preferredLocale;
    private String name;
    private String language;
    private String newPassword;
    private String confirmPassword;
    private Date passwordChangeDate;
    private MenuModel staffMenuModel;
    
    
    public StaffEditAccountManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        /* pre-load values from session and database */
        // get account ID values from session
        if (ctx.getExternalContext().getSessionMap().containsKey("staffAccountId")) {
            staffAccountId = (Long) ctx.getExternalContext().getSessionMap().get("staffAccountId");
            System.out.println("Account ID: " + staffAccountId);
            // get other session values required
            staffMenuModel = (MenuModel) ctx.getExternalContext().getSessionMap().get("staffMenuModel");
        }
        else {
            // redirect back to login
            PageRedirectorBean redirect = new PageRedirectorBean();
            redirect.goStaffLoginPageAtPreviousLevel();
        }
        /* END load values from session and database */      
    }
    
    public void initView(PhaseEvent event) {
        try {
            // set language for all MANAGERS for all THROWABLE EXCEPTIONS in backend
            // to use this line everytime possible language changes
            staffAccountMgr.setSelectedLocale(preferredLocale);
            // get data from backend, exception alrd thrown by backend if these methods fail.
            
            staffAcctState = staffAccountMgr.getStaffAccountByID(staffAccountId);
            
            username = staffAcctState.getUsername();
            currentPassword = staffAcctState.getPassword();
            
            lastname = staffAcctState.getStaff().getLastName();
            firstname = staffAcctState.getStaff().getFirstName();
            name = lastname + " " + firstname;
            
            Gender savedGender = staffAcctState.getStaff().getGender();
            
            Date savedBirthdate = staffAcctState.getStaff().getBirthdate();
//            birthdate = localization.getLocalizedDateFormat(savedBirthdate);
            
            Country savedCountry = staffAcctState.getStaff().getCountry();
//            country = localization.getLocalizedCountry(savedCountry);
            
            locale = staffAcctState.getStaff().getLocale();
            language = localization.getLocalizedLanguage(SupportedLocale.getLanguageBySupportedLocale(locale));
            
            email = staffAcctState.getStaff().getEmail();
                    
            if (staffAcctState.getStaff().getEmailSecondary().isEmpty()) {
                emailSecondary = "-";
            }
            else {
                emailSecondary = staffAcctState.getStaff().getEmailSecondary();
            }
        }
        catch (ExistException ex) {
//            System.out.println(ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
        }
    }
    

    public void updateStaffDetails(ActionEvent event) {
        // set language for all MANAGERS for all THROWABLE EXCEPTIONS in backend
        // to use this line everytime possible language changes
        staffAccountMgr.setSelectedLocale(preferredLocale);
    }
    
//    public void saveNewEvent(ActionEvent event)
//    {
//        Timestamp tsStartDateTime = Timestamp.valueOf(startDateTime);
//        Timestamp tsEndDateTime = Timestamp.valueOf(endDateTime);
//        Long venueId = Long.valueOf(venue);
//        Long systemUserId = Long.valueOf(systemUser);
//        try
//        {
//            newEventId = eventSessionBean.addNewEvent(eventName, tsStartDateTime, tsEndDateTime, venueId, systemUserId);
//            statusMessage = "New Event Saved Successfully";
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Event Result: " + statusMessage + " (New Event ID is " + newEventId + ")", ""));
//        }
//        catch (VenueConflictException vex)
//        {
//            statusMessage = "Venue Conflict Exception";
//            newEventId = -1L;
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Add New Event Result: " + statusMessage, ""));
//        }
//        catch(Exception ex)
//        {
//            ex.printStackTrace();
//        }
//    }

    public StaffAccountManagerLocal getStaffAccountMgr() {
        return staffAccountMgr;
    }

    public void setStaffAccountMgr(StaffAccountManagerLocal staffAccountMgr) {
        this.staffAccountMgr = staffAccountMgr;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }
    
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public SystemMessagesLocal getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesLocal systemMessages) {
        this.systemMessages = systemMessages;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEmailSecondary() {
        return emailSecondary;
    }

    public void setEmailSecondary(String emailSecondary) {
        this.emailSecondary = emailSecondary;
    }

    public Long getStaffAccountId() {
        return staffAccountId;
    }

    public void setStaffAccountId(Long staffAccountId) {
        this.staffAccountId = staffAccountId;
    }

    public StaffAccountState getStaffAcctState() {
        return staffAcctState;
    }

    public void setStaffAcctState(StaffAccountState staffAcctState) {
        this.staffAcctState = staffAcctState;
    }

    public String getCurrentPassword() {
        return currentPassword;
    }

    public void setCurrentPassword(String currentPassword) {
        this.currentPassword = currentPassword;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }
    
}
